package com.fengzai.utils;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Created by  峰仔  on 2017-08-10
 */
public class ZkUtil {
    private static String ZOOKEEPER_SERVER = "192.168.66.66:2181";

    /**
     * 返回一个 CuratorFramework 实例
     *
     * @param node 以后的所有操作都在此节点之后
     * @return
     */
    public static CuratorFramework getInstace(String node) {
        node = node == null ? "" : node;
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString(ZOOKEEPER_SERVER).connectionTimeoutMs(10000).sessionTimeoutMs(10000).retryPolicy(new ExponentialBackoffRetry(1000, 3)).namespace(node).build();
        curatorFramework.start();
        return curatorFramework;
    }

    /**
     * 返回一个 CuratorFramework 实例
     * 默认监听根节点
     *
     * @return
     */
    public static CuratorFramework getInstace() {
        return getInstace(null);
    }
}
